/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func rightSideView(root *TreeNode) []int {
    ans := []int{}
    if root == nil {
        return ans
    }
    // 初始化
    res := [][]int{}
    tempArr := []int{}
    queue := list.New()
    queue.PushBack(root)

    // 处理队列
    for queue.Len() > 0 {
        length := queue.Len()
        for i:=0; i<length; i++ {
            node := queue.Remove(queue.Front()).(*TreeNode)
            tempArr = append(tempArr, node.Val)
            if node.Left != nil {
                queue.PushBack(node.Left)
            }
            if node.Right != nil {
                queue.PushBack(node.Right)
            }
            continue
        }
        res = append(res, tempArr)
        tempArr = []int{}
    }
    for i:=0; i<len(res); i++ {
        ans = append(ans, res[i][len(res[i])-1])
    }

    return ans
}